$(function () {
    $("#ac").accordion({
        fit: true,
        background:'red',
    })

    $("#gender").combobox('setValue','1');
    $("#classify").combobox('setValue','1');
    $("#carsize").combobox('setValue','1');
    $("#pro_datagrid").datagrid({
        width: 1200,
        height: 300,
        fit:true,
        fitColumns:true,
        columns: [[
            {field: 'x', checkbox: true},
            {field: 'id', title: '编号', hidden: true},
            {field: 'sn', title: '保险编号', width: 100},
            {field: 'name', title: '保险产品', width: 100},
            {field: 'annualfee', title: '保险年费', width: 100}
        ]],
        //新增
        border:false,
        cls:"table table-very table-basic"
    })

    var obj = {
        add:function () {
            $("#client_form").form("submit", {
                url: '/insurancebill/save',
                onSubmit: function (param) {
                    var ids = $("#pro_datagrid").datagrid('getSelections');
                    if(ids){
                        for(var i = 0;i<ids.length;i++){
                            param["productIds["+i+"]"]=ids[i].id;
                        }
                    }
                    var appId = $("#appId").val();
                    if(appId){
                        param["applicant.id"] =appId;
                    }
                    param["applicant.name"] = $("#name").textbox("getValue");
                    param["applicant.gender"] = $("#gender").textbox("getValue");
                    param["applicant.identityCard"] = $("#identityCard").textbox("getValue");
                    param["applicant.phone"] = $("#phone").textbox("getValue");
                    param["applicant.address"] = $("#address").textbox("getValue");
                    param["car.brand"] = $("#brand").textbox("getValue");
                    param["car.number"] = $("#number").textbox("getValue");
                    param["car.displacement"] = $("#displacement").textbox("getValue");
                    param["car.classify"] = $("#classify").combobox("getValue");
                    param["car.buydate"] = $("#buyDate").datebox("getValue");
                    param["car.evaluate"] = $("#evaluate").textbox("getValue");
                    param["car.carOwner"] = $("#carname").textbox("getValue");
                    param["car.model"] = $("#model").textbox("getValue");
                    console.log($("#totalAmount").text());
                    param["totalPrice"]=$("#totalAmount").text();
                    param["limitYear"]=$("#burningTime").val();
                },
    /*            success: function () {
                    $.messager.progress('close');	// 如果提交成功则隐藏进度条
                }*/
                success: function (data) {
                    //把字符串转换为json
                    var data = $.parseJSON(data);
                    if (!data.succeed) {
                        $.messager.alert('温馨提示', data.errorMsg);
                    } else {
                        $.messager.alert('温馨提示', "保存成功");
                    }
                }
            })
        }
    }
    $("#submitBtn").click(function () {
        obj["add"]();
    })



    //算钱的
    $("#burningTime").textbox({
        onChange:function () {
            var totalAmount=0;
            var rows = $("#pro_datagrid").datagrid('getSelections');
            if(rows){
                for(var i = 0;i<rows.length;i++){
                    totalAmount=rows[i].annualfee * $("#burningTime").val() + totalAmount;
                }
            }
            $("#totalAmount").text(totalAmount);
        }
    })
})



function vip() {
    $("#client_dialog").dialog({
        title: '客户信息',
        width: 700,
        height: 500,
        buttons: "#save"
    })

    $("#clientDg").datagrid({
        url: '/client/query',
        fit: true,
        singleSelect: true,
        fitColumns: true,
        columns: [[
            {field: 'x', checkbox: true},
            {field: 'id', title: '编号', hidden: true},
            {field: 'name', title: '客户', width: 100},
            {field: 'age', title: '年龄', width: 100},
            {
                field: 'gender', title: '性别', width: 100, formatter: function (v, r, i) {
                    return v == 1 ? '男' : '女'
                }
            },
            {field: 'address', title: '客户地址', width: 100},
            {field: 'email', title: '邮箱 ', width: 100},
            {field: 'identityCard', title: '身份证号码 ', width: 100}
        ]],
        //新增
        border:false,
        cls:"table table-very table-basic"
    })



}

function loadClient() {
    var row = $("#clientDg").datagrid("getSelected");
    $("#client_form").form("load", row);
    $("#client_dialog").dialog("close");
}

function car() {
    $("#car_dialog").dialog({
        title: '客户信息',
        width: 700,
        height: 500,
        buttons: "#saveC"
    })

    $("#carDg").datagrid({
        url: '/car/query',
        fit: true,
        singleSelect: true,
        fitColumns: true,
        columns: [[
            {field: 'x', checkbox: true},
            {field: 'id', title: '编号', hidden: true},
            {field: 'brand', title: '品牌', width: 100},
            {field: 'model', title: '车型号', width: 100},
            {field: 'displacement', title: '排气量', width: 100},
            {field: 'evaluate', title: '市场估价', width: 100},
            {
                field: 'classify', title: '分类', width: 100, formatter: function (v, r, i) {
                    return v == 0 ? "载人" : "载物";
                }
            },
            {
                field: 'carsize', title: '车载大小', width: 100, formatter: function (v, r, i) {
                    if (v == 0) {
                        return "小型";
                    } else if (v == 1) {
                        return "中型"
                    } else if (v == 2) {
                        return "大型"
                    } else if (v == 3) {
                        return "超大型"
                    } else {
                        return "其他类型";
                    }
                }
            }
        ]],
        //新增
        border:false,
        cls:"table table-very table-basic"
    })
}

function loadCar() {
    var row = $("#carDg").datagrid("getSelected");
    $("#car_form").form("load", row);
    $("#car_dialog").dialog("close");
}


function addProduct() {
    var id = $("#agency").combobox("getValue")
    if(id){
        $("#pro_datagrid").datagrid('options').url = '/product/queryByOrganizationId?id='+id;
        $("#pro_datagrid").datagrid('reload');
        $("#pro_datagrid").datagrid('select');
    }else{
        $.messager.alert("温馨提示","请选择一个机构")
    }

}